<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目时间" prop="projectTime">
        <el-date-picker clearable
          v-model="queryParams.projectTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="项目开始时间"
          end-placeholder="项目结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目招募开始时间" prop="publicTime">
        <el-date-picker clearable
          v-model="queryParams.publicTime"
          type="datetimerange"
          start-placeholder="项目招募开始时间"
          end-placeholder="项目招募结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="主要联系人" prop="projecContacts" v-if="false">
        <el-input
          v-model="queryParams.projecContacts"
          placeholder="请输入主要联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要联系人电话" prop="projecPhone" v-if="false">
        <el-input
          v-model="queryParams.projecPhone"
          placeholder="请输入主要联系人电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务类别" prop="projectType">
        <el-select v-model="queryParams.projectType" placeholder="请选择服务类别" clearable>
          <el-option
            v-for="dict in cl_team_service_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务对象" prop="serviceObject">
        <el-select v-model="queryParams.serviceObject" placeholder="请选择服务对象" clearable>
          <el-option
            v-for="dict in c_service_object"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="完成情况" prop="jobDone" v-if="false">
        <el-input
          v-model="queryParams.jobDone"
          placeholder="请输入完成情况"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择项目状态" clearable>
          <el-option
            v-for="dict in cl_pexamine_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="false">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ndpf:projects:add']"
        >新增项目</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleProjectInfo"
          v-hasPermi="['ndpf:projects:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ndpf:projects:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ndpf:projects:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="false"/>
      <el-table-column label="项目表id" align="center" prop="projectId" v-if="false"/>
      <el-table-column label="团体ID" align="center" prop="teamId" v-if="false"/>
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="所在区域" align="center" prop="area" :formatter="formatRegion" show-overflow-tooltip v-if="false"/>
      <el-table-column label="所在区域" align="center" prop="areaName" show-overflow-tooltip/>
      <el-table-column label="协议,用户须知" align="center" prop="projectProtocol" v-if="false"/>
      <el-table-column label="项目描述" align="center" prop="projectDesc" v-if="false"/>
      <el-table-column label="项目开始时间" align="center" prop="startProject" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startProject) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目结束时间" align="center" prop="endProject" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endProject) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目招募开始时间" align="center" prop="startPublic" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startPublic) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目招募结束时间" align="center" prop="endPublic" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endPublic) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审计部门" align="center" prop="auditGroup" v-if="false"/>
      <el-table-column label="邮箱" align="center" prop="email" v-if="false"/>
      <el-table-column label="主要联系人" align="center" prop="projecContacts" />
      <el-table-column label="主要联系人电话" align="center" prop="projecPhone" />
      <el-table-column label="项目海报" align="center" prop="poster" width="100" v-if="false">
        <template #default="scope">
          <image-preview :src="scope.row.poster" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="服务类别" align="center" prop="projectType" v-if="false">
        <template #default="scope">
          <dict-tag :options="cl_team_service_type" :value="scope.row.projectType"/>
        </template>
      </el-table-column>
      <el-table-column label="服务对象" align="center" prop="serviceObject" v-if="false">
        <template #default="scope">
          <dict-tag :options="c_service_object" :value="scope.row.serviceObject"/>
        </template>
      </el-table-column>
      <el-table-column label="招募要求" align="center" prop="require" v-if="false"/>
      <el-table-column label="完成情况" align="center" prop="jobDone" v-if="false"/>
      <el-table-column label="所在城市" align="center" prop="city" v-if="false"/>
      <el-table-column label="项目照片" align="center" prop="projectPhoto" width="100" v-if="false">
        <template #default="scope">
          <image-preview :src="scope.row.projectPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center" prop="score" v-if="false"/>
      <el-table-column label="服务时长" align="center" prop="serviceTime" />
      <el-table-column label="已报名的人数" align="center" prop="number" v-if="false"/>
      <el-table-column label="招募人数要求" align="center" prop="numberDesc" v-if="false"/>
      <el-table-column label="限制招募人数" align="center" prop="numberLimit" v-if="false"/>
      <el-table-column label="地址" align="center" prop="address" v-if="false"/>
      <el-table-column label="经度" align="center" prop="longitude" v-if="false"/>
      <el-table-column label="纬度" align="center" prop="latitude" v-if="false"/>
      <el-table-column label="经纬度" align="center" prop="lonlat" v-if="false"/>
      <el-table-column label="项目二维码" align="center" prop="projectQrcode" width="100" v-if="false">
        <template #default="scope">
          <image-preview :src="scope.row.projectQrcode" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="cl_pexamine_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" v-if="false"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220px">
        <template #default="scope">
          <el-button type="success" plain  @click="handleExaminePass(scope.row,'1')" v-hasPermi="['ndpf:team:edit']" size="small" v-if="scope.row.status === '1'">通过</el-button>
          <el-button type="warning" plain  @click="handleExamineRefuse(scope.row,'2')" v-hasPermi="['ndpf:team:edit']" size="small" v-if="scope.row.status === '1'">驳回</el-button>
          <!-- <el-button type="success" plain  @click="handleExamine(scope.row,'1')" v-hasPermi="['ndpf:team:remove']" size="small" v-if="scope.row.status === '1'">启用</el-button>
          <el-button type="warning" plain  @click="handleExamine(scope.row,'4')" v-hasPermi="['ndpf:team:remove']" size="small" v-if="scope.row.status === '1'">停用</el-button>   -->
          <el-button  type="primary"  @click="handleProjectInfo(scope.row)" v-hasPermi="['ndpf:projects:edit']" size="small">查看</el-button>
          <el-button  type="danger"   @click="handleDelete(scope.row)" v-hasPermi="['ndpf:projects:remove']" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1200px" append-to-body>
      <el-form ref="projectsRef" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="8" v-if="false">
            <el-form-item label="团体ID" prop="teamId" >
              <el-input v-model="form.teamId" placeholder="请输入团体ID" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="主要联系人" prop="projecContacts">
              <el-input v-model="form.projecContacts" placeholder="请输入主要联系人" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="主要联系人电话" prop="projecPhone">
              <el-input v-model="form.projecPhone" placeholder="请输入主要联系人电话" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="服务类别" prop="projectType">
              <el-select v-model="form.projectType" placeholder="请选择服务类别" multiple disabled>
                <el-option
                  v-for="dict in cl_team_service_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="服务对象" prop="serviceObject">
              <el-select v-model="form.serviceObject" placeholder="请选择服务对象" multiple disabled>
                <el-option
                  v-for="dict in c_service_object"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="项目描述" prop="projectDesc">
              <el-input v-model="form.projectDesc" type="textarea" :autosize="{ minRows: 6, maxRows: 20 }" placeholder="" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="招募要求" prop="require">
              <el-input v-model="form.require" type="textarea" :autosize="{ minRows: 6, maxRows: 20 }" placeholder="" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目时间" prop="startProject" >
              <el-date-picker clearable readonly
                v-model="form.startProject"
                type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择项目时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="false">
            <el-form-item label="项目结束时间" prop="endProject" >
              <el-date-picker clearable readonly
                v-model="form.endProject"
                type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择项目结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目招募时间" prop="startPublic">
              <el-date-picker clearable readonly
                v-model="form.startPublic"
                type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择项目招募时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="false">
            <el-form-item label="项目招募结束时间" prop="endPublic">
              <el-date-picker clearable readonly
                v-model="form.endPublic"
                type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择项目招募结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="招募范围" prop="recruitScope">
              <el-select
                v-model="form.recruitScope"
                filterable
                multiple
                placeholder="招募范围不选择即为（公开招募），亦可选择指定团队下的志愿者可以报名"
                style="width: 300px"
                clearable
              >
                <!-- <el-option key="0" label="公开招募" value = "0"/> -->
                <el-option
                  v-for="item in teamList"
                  :key="item.teamId"
                  :label="item.companyName"
                  :value="item.teamId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="">招募范围不选择即为（公开招募），亦可选择指定团队下的志愿者可以报名</el-form-item>
          </el-col>
          <el-col :span="8"  v-if="false">
            <el-form-item label="所在城市" prop="city">
              <el-input v-model="form.city" placeholder="" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="招募人数要求" prop="numberDesc">
              <!-- <el-input v-model="form.numberDesc" placeholder="请输入招募人数要求" /> -->
              <el-input-number v-model="form.numberDesc" :min="1" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="限制招募人数" prop="numberLimit">
              <!-- <el-input v-model="form.numberLimit" placeholder="请输入限制招募人数" /> -->
              <el-input-number v-model="form.numberLimit" :min="1" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所在区域" prop="areaName">
              <el-input v-model="form.areaName" placeholder="请输入所在区域" readonly/>
               <!-- <el-cascader disabled
                v-model="form.area"
                :options="addressOptions"
                :props="cascaderProps"
                placeholder="请选择市/区/街道/居委会"
                clearable
                style="width:800px"
              /> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入地址" readonly/>
          </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目二维码" prop="projectQrcode">
              <image-preview :src="form.projectQrcode" :width="170" :height="170"/>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="项目海报" prop="poster">
              <image-preview :src="form.poster" :width="170" :height="170"/>
            </el-form-item>    
          </el-col>   
          <el-col :span="24">
            <el-form-item label="项目照片" prop="projectPhoto">
              <image-preview :src="form.projectPhoto" :width="170" :height="170"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="完成情况" prop="jobDone">
              <el-input v-model="form.jobDone" type="textarea" placeholder="请输入完成情况" :autosize="{ minRows: 6, maxRows: 20 }"/>
            </el-form-item>  
          </el-col>
          <el-form-item label="经度" prop="longitude" v-if="false">
            <el-input v-model="form.longitude" placeholder="请输入经度" />
          </el-form-item>
          <el-form-item label="纬度" prop="latitude" v-if="false">
            <el-input v-model="form.latitude" placeholder="请输入纬度" />
          </el-form-item>
          <el-form-item label="经纬度" prop="lonlat" v-if="false">
            <el-input v-model="form.lonlat" placeholder="请输入经纬度" />
          </el-form-item>
          <el-form-item label="审计部门" prop="auditGroup">
            <el-input v-model="form.auditGroup" placeholder="" readonly/>
          </el-form-item>
          <el-form-item label="项目状态" prop="status">
            <el-select v-model="form.status" placeholder="" disabled> 
              <el-option
                v-for="dict in cl_pexamine_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="协议,用户须知" prop="projectProtocol" v-if="false">
            <el-input v-model="form.projectProtocol" placeholder="请输入协议,用户须知" />
          </el-form-item>
          <el-form-item label="备注" prop="remark" v-if="false">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="warning" @click="handleExaminePass(form)" v-if="form.status == '1'">通过</el-button>
          <el-button type="success" @click="handleExamineRefuse(form)" v-if="form.status == '1'">驳回</el-button>
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Projects">
import { listProjects, getProjects, delProjects, addProjects, updateProjects, listExamineProjects, examineProjectPass, examineProjectRefuse } from "@/api/ndpf/projects";
import { listRegion, getRegion, delRegion, addRegion, updateRegion } from "@/api/ndpf/region";

const { proxy } = getCurrentInstance();
const { c_service_object, cl_team_service_type, cl_pexamine_status } = proxy.useDict('c_service_object', 'cl_team_service_type', 'cl_pexamine_status');

const projectsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const projectTime = ref([]);
const publicTime = ref([]);
const selectedAddress = ref([]);
const addressOptions = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectName: null,
    startProject: null,
    endProject: null,
    startPublic: null,
    endPublic: null,
    projecContacts: null,
    projecPhone: null,
    projectType: null,
    serviceObject: null,
    jobDone: null,
    status: null,
    projectTime: null,
    publicTime: null,
  },
  cascaderProps: {
    expandTrigger: 'hover', // 鼠标悬停展开
    checkStrictly: true, // 可以选择任意一级
    value: 'regionId',
    label: 'regionName',
    children: 'children'
  },
  rules: {
    teamId: [
      { required: false, message: "团体ID不能为空", trigger: "blur" }
    ],
    projectName: [
      { required: true, message: "项目名称不能为空", trigger: "blur" }
    ],
    projectDesc: [
      { required: true, message: "项目描述不能为空", trigger: "blur" }
    ],
    startProject: [
      { required: true, message: "项目开始时间不能为空", trigger: "blur" }
    ],
    endProject: [
      { required: true, message: "项目结束时间不能为空", trigger: "blur" }
    ],
    startPublic: [
      { required: true, message: "项目招募开始时间不能为空", trigger: "blur" }
    ],
    endPublic: [
      { required: true, message: "项目招募结束时间不能为空", trigger: "blur" }
    ],
    projecContacts: [
      { required: true, message: "主要联系人不能为空", trigger: "blur" }
    ],
    projecPhone: [
      { required: true, message: "主要联系人电话不能为空", trigger: "blur" }
    ],
    projectType: [
      { required: true, message: "服务类别不能为空", trigger: "change" }
    ],
    serviceObject: [
      { required: true, message: "服务对象不能为空", trigger: "change" }
    ],
    city: [
      { required: false, message: "所在城市不能为空", trigger: "blur" }
    ],
    serviceTime: [
      { required: true, message: "服务时长不能为空", trigger: "blur" }
    ],
    numberDesc: [
      { required: true, message: "招募人数要求不能为空", trigger: "blur" }
    ],
    numberLimit: [
      { required: true, message: "限制招募人数不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "地址不能为空", trigger: "blur" }
    ],
    area: [
      { required: true, message: "所在区域不能为空", trigger: "blur" }
    ],
    require: [
      { required: true, message: "招募要求不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules, cascaderProps } = toRefs(data);

/** 查询项目信息列表 */
function getList() {
  loading.value = true;
  listExamineProjects(queryParams.value).then(response => {
    projectsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    projectId: null,
    teamId: null,
    projectName: null,
    projectProtocol: null,
    projectDesc: null,
    startProject: null,
    endProject: null,
    startPublic: null,
    endPublic: null,
    auditGroup: null,
    email: null,
    projecContacts: null,
    projecPhone: null,
    poster: null,
    projectType: null,
    serviceObject: null,
    require: null,
    jobDone: null,
    city: null,
    projectPhoto: null,
    score: null,
    serviceTime: null,
    number: null,
    numberDesc: null,
    numberLimit: null,
    address: null,
    area: null,
    longitude: null,
    latitude: null,
    lonlat: null,
    projectQrcode: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("projectsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  if(queryParams.value.projectTime && queryParams.value.projectTime.length === 2){
    queryParams.value.startProject = queryParams.value.projectTime[0];
    queryParams.value.endProject = queryParams.value.projectTime[1];
  }
  if(queryParams.value.publicTime && queryParams.value.publicTime.length === 2){
    queryParams.value.startPublic = queryParams.value.publicTime[0];
    queryParams.value.endPublic = queryParams.value.publicTime[1];
  }
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.startProject = null;
  queryParams.value.endProject = null;
  queryParams.value.startPublic = null;
  queryParams.value.endPublic = null;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.projectId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加项目信息";
}

/** 修改按钮操作 */
function handleProjectInfo(row) {
  reset();
  const _projectId = row.projectId || ids.value
  getProjects(_projectId).then(response => {
    form.value = response.data;
    form.value.projectType = (form.value.projectType || '').split(',');
    form.value.serviceObject = (form.value.serviceObject || '').split(',');
    form.value.area = (form.value.area || '').split(',');
    form.value.startProject = [form.value.startProject,form.value.endProject];
    form.value.startPublic = [form.value.startPublic,form.value.endPublic];
    open.value = true;
    title.value = "查看项目信息";
  });
}

/** 提交按钮 */
function submitForm(st) {
  form.value.status = st;
  proxy.$refs["projectsRef"].validate(valid => {
    if (valid) {
      form.value.projectType = form.value.projectType.join(",");
      form.value.serviceObject = form.value.serviceObject.join(",");
      form.value.endProject = form.value.startProject[1];
      form.value.startProject = form.value.startProject[0];
      form.value.endPublic = form.value.startPublic[1];
      form.value.startPublic = form.value.startPublic[0];
      form.value.area = form.value.area.join(",");
      if (form.value.projectId != null) {
        updateProjects(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProjects(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _projectName = row.projectName
  const _projectIds = row.projectId || ids.value;
  proxy.$modal.confirm('是否确认删除项目名称为"' + _projectName + '"的数据项？').then(function() {
    return delProjects(_projectIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ndpf/projects/export', {
    ...queryParams.value
  }, `projects_${new Date().getTime()}.xlsx`)
}


/** 查询区域信息列表 */
function getRegionList() {
  listRegion(queryParams.value).then(response => {
    addressOptions.value = buildTree(response.data);
  });
}

function buildTree(regions, parentId = 0) {
  const tree = [];
  regions.forEach(region => {
    if (Number(region.parentId) === Number(parentId)) {
      const children = buildTree(regions, region.regionId);
      if (children.length > 0) {
        region.children = children;
      }
      tree.push(region);
    }
  });
  return tree;
}

// 格式化显示地区
function formatRegion(row, column, cellValue) {
  if (!cellValue || cellValue.length === 0) return ''
  const labels = []
  let currentOptions = addressOptions.value;
  
  for (const value of cellValue) {
    const found = currentOptions.find(item => item.regionId === value)
    console.log("found:" + found); // 调试用，可删除
    if (found) {
      labels.push(found.regionName)
      currentOptions = found.children || []
    } else {
      
    }
  }
  
  return labels.join(' / ')
}

/** 审核通过操作 */
function handleExaminePass(row) {
  const _projectName = row.projectName
  const _projectIds = row.projectId || projectIds.value
  proxy.$modal.confirm('是否确认"' + _projectName + '"审批通过').then(function() {
    return examineProjectPass(_projectIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("审批成功");
    cancel();
  }).catch(() => {});
}

/** 审核拒绝操作 */
function handleExamineRefuse(row) {
  const _projectName = row.projectName
  const _projectIds = row.projectId || projectIds.value
  proxy.$modal.confirm('是否确认"' + _projectName + '"审批驳回').then(function() {
    return examineProjectRefuse(_projectIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("驳回成功");
    cancel();
  }).catch(() => {});
}


getList();
//getRegionList();
</script>